<?php

namespace common\models\product;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\product\Product;

/**
 * ProductSearch represents the model behind the search form about `common\models\product\Product`.
 */
class ProductSearch extends Product
{
    public $goods_name;
    public $is_on_sale;
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'goods_id', 'number_stock', 'number_lock', 'is_buy_min_disable', 'is_buy_setp_disable', 'weight', 'supplier_id','is_on_sale'], 'integer'],
            [['sn', 'sn_purchase', 'sn_intel', 'goods_attr', 'goods_attr_str', 'img', 'name_short', 'slot','goods_name'], 'safe'],
            [['price_market', 'price_cost'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        
        $query = Product::find();
        $query->joinWith(['goods']);
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'goods_id' => $this->goods_id,
            'number_stock' => $this->number_stock,
            'number_lock' => $this->number_lock,
            'price_market' => $this->price_market,
            'price_cost' => $this->price_cost,
            'is_buy_min_disable' => $this->is_buy_min_disable,
            'is_buy_setp_disable' => $this->is_buy_setp_disable,
            'weight' => $this->weight,
            Product::tableName().'.supplier_id' => $this->supplier_id,
            'is_on_sale' => $this->is_on_sale,
        ]);

        $query->andFilterWhere(['like', 'sn', $this->sn])
            ->andFilterWhere(['like', 'sn_purchase', $this->sn_purchase])
            ->andFilterWhere(['like', 'sn_intel', $this->sn_intel])
            ->andFilterWhere(['like', 'goods_attr', $this->goods_attr])
            ->andFilterWhere(['like', 'goods_attr_str', $this->goods_attr_str])
            ->andFilterWhere(['like', 'img', $this->img])
            ->andFilterWhere(['like', 'name_short', $this->name_short])
            ->andFilterWhere(['like', 'slot', $this->slot])
            ->andFilterWhere(['like', 'name', $this->goods_name]);

        return $dataProvider;
    }
}
